package top.wardyan.wdev.dbassist.action.dic;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import top.wardyan.util.Msg;
import top.wardyan.wdev.dbassist.model.YTDic;

public class DicEditShell extends DicAddShell{

	private YTDic dic = null;
	private int sID;
	
	public DicEditShell() {
		this(0);
	}
	
	public DicEditShell(int sID) {
		super();
		self = this;
		this.sID = sID;
		dic = dda.dicService.findByID(sID);
		if(dic == null) {
			Msg.show(self, "错误", "未找到记录:" + sID);
			self.dispose();
			return;
		}
		this.show();
		
		self.btnReset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				((DicEditShell)self).show();
			}
		});
		this.txtSID.setEditable(false);
		this.txtSID.setToolTipText("不可编辑");
	}

	@Override
	protected void init() {}
	
	public void show() {
		if(dic != null) {
			txtSID.setText(dic.getSID().toString());
			txtXName.setText(dic.getXName());
			txtXField.setText(dic.getXField());
			txtXField2.setText(dic.getXField2());
			txtXField3.setText(dic.getXField3());
			txtXNote.setText(dic.getXNote());
			//cmbStatus.setText(dic.getSStatus().toString());
			cmbStatus.select(dic.getSStatus() - 1);
		}
	}
	
	@Override
	protected void save() {
		if(checkInput()) {
			Integer sID = Integer.valueOf(txtSID.getText());
			YTDic dic = dda.dicService.findByID(sID); 
			if(dic == null) {
				this.error("未找到对象");
				txtSID.setFocus();
				return;
			}
			dic.setXName(txtXName.getText()).setXField(txtXField.getText()).setXField2(txtXField2.getText())
					.setXField3(txtXField3.getText()).setXNote(txtXNote.getText()).setSStatus(Integer.valueOf(String.valueOf(cmbStatus.getText().charAt(0))))
					;
			if(dda.dicService.update(dic)) {
				Msg.show(self, "保存", "保存成功");
				self.dispose();
			}else {
				this.error("保存失败");
			}
		}
	}
	
	
}
